import {getInfo, balancePay, wxPrepay, checkIfNeedPwd} from '@mall/api/order'
import md5 from '@src/common/md5'
import $math from '@src/common/math'


Component({

  /**
   * 组件的属性列表
   */

  properties: {
    hiddenPay: {
      type: Boolean,
      value: true
    },
    payMoney: {
      type: Number,

    },
    goodName: {
      type: String,
      value: '预定-江景大床房'
    },
    //支付类型
    payType: {
      type: Number
    },
    //会员余额
    personBalance: {
      type: Number
    },
    //订单编号
    orderNo: {
      type: String
    },
    //是否支持余额支付
    supportBALANCE: {
      type: Boolean,
      value: false
    },
    //是否支持微信支付
    supportWX: {
      type: Boolean,
      value: false
    }
  },
  options: {

    addGlobalClass: true
  },

  /**
   * 组件的初始数据
   */
  data: {
    //0 余额支付 1微信支付
    // payType: 0,
    personBalance: null,
    //是否支付成功
    ispay: false,
    hiddenPassword: true,

    //
    Length: 6,        //输入框个数
    isFocus: false,    //聚焦
    inputValue: "",        //输入的内容
    inputValueTrue: "",
    isPassword: true, //是否密文显示 true为密文， false为明文。
    needPassword: true,
    isClick: false,
  },

  /**
   * 组件生命周期
   */
  lifetimes: {
    created: function (val) {

    },
    attached: function (val) {
      //console.log('md5md5md5md5', md5)

    },
    ready: function (val) {


    },
    observers: {
      'supportBALANCE': function () {
        console.log("=-==-=-=-=-")
        console.log("this.data.supportBALANCE")
        if (this.data.personBalance < this.data.payMoney) {
          this.data.payType = 1
          // this.setData({
          //   payType: 1
          // })
        } else {
          this.data.payType = 0
          // this.setData({
          //   payType: 0
          // })
        }
      }
    },


    moved: function () {

    },
    detached: function () {

    },
  },

  /**
   * 组件所在页面生命周期
   */
  pageLifetimes: {},

  /**
   * 组件的方法列表
   */
  methods: {
    onShow(){
      // debugger
      // getInfo().then(res => {
      //   console.log('会员余额res', res);
      //   console.log()
      //   debugger
      //   let personBalance = (res.data.balance + res.data.giftBalance - res.data.lockedBalance - res.data.lockedGiftBalance) / 100;
      //   this.setData({
      //     personBalance: personBalance,
      //     payType: 0
      //   })
      //   // console.log('personBalance', personBalance)
      // })
    },
    setHiddentrue() {
      let orderNo = this.data.orderNo
      this.setData({
        hiddenPay: true
      })
      setTimeout(()=>{
        this.setData({isClick: false})
      }, 600)
      //向父组件发出隐藏事件
      this.triggerEvent("setHiddentrueFunc")
    },
    //影藏密码输入框
    setHiddenPassword() {
      let orderNo = this.data.orderNo
      this.setData({
        hiddenPassword: true
      })
      // wx.navigateTo({
      //   url: '/pages/shop-mall/order-detail/index?orderNo=' + orderNo
      // })
    },
    selectPayType(e) {
      if (e.currentTarget.dataset.type === 0 && (this.data.personBalance >= this.data.payMoney)) {
        console.log('0')
        this.setData({
          payType: 0
        })
      } else if (e.currentTarget.dataset.type === 1) {
        this.setData({
          payType: 1
        })
      }
    }
    ,
    //点击余额支付按钮
    async balancePayment() {
      const self = this;
      console.log('isClick', this.data.isClick)
      if (this.data.isClick) {
        return;
      } else {
        self.setData({isClick: true})
        setInterval(() => {
          self.setData({isClick: false})
        }, 15000)
      }
      if (!self.data.personBalance > 0) {
        return;
      }
      // console.log("self.data.payMoney", self.data.payMoney * 100)
      let money = $math.mul(self.data.payMoney, 100)
      // let money = self.data.payMoney
      console.log('money', money)

      await checkIfNeedPwd(money).then(res => {
        console.log('res', res)
        if (res.code === 0) {
          self.setData({
            needPassword: res.data.needPassword
          })

        }
      })
      if (self.data.needPassword) {
        this.setData({
          hiddenPassword: false,
          hiddenPay: true,
          isFocus: true
        })
      } else {
        self.balancePayFun();
      }
    },
    //发起微信支付
    wxpayment() {
      const self = this;
      if (this.data.isClick) {
        return;
      } else {
        self.setData({isClick: true})
        setInterval(() => {
          self.setData({isClick: false})
        }, 15000)
      }
      let orderNo = this.data.orderNo
      wxPrepay({orderNo: orderNo}).then(res => {
        console.log("微信预支付参数", res);
        if (res.code === 0) {
          wx.requestPayment({
            timeStamp: res.data.payTimestamp,
            nonceStr: res.data.payNonceStr,
            package: res.data.payPackage,
            signType: res.data.paySignType,
            paySign: res.data.paySignature,
            success(res) {
              console.log('res 支付成功', res);
              self.setHiddentrue()
            },
            fail(res) {
              console.log('res 支付失败', res)
              self.setHiddentrue()
            }
          })
        } else {
          this.selectComponent("#toast").show({icon: "error", txt: res.msg, type: 'info', time: 2000});
          setTimeout(res => {
            self.setHiddentrue()
          }, 2000)
        }
      }).catch(() => {
        this.selectComponent("#toast").show({icon: "error", txt: "请求异常, 支付失败", type: 'info', time: 2000});
        setTimeout(res => {
          self.setHiddentrue()
        }, 1500)
      })
    },
    //获取输入框的值
    getInputValue(e) {
      const that = this;
      that.setData({
        inputValue: e.detail.value
      })
      if (that.data.inputValue.length === 6) {
        this.balancePayFun()
      }
    },
    //发起余额支付
    balancePayFun() {
      let orderNo = this.data.orderNo;
      let inputValue;
      if (this.data.inputValue) {
        inputValue = self.data.inputValue//md5.md5(self.data.inputValue) //注释md5
      }
      balancePay({orderNo: orderNo, password: inputValue}).then(res => {
        this.setData({hiddenPassword: true})
        if (res.code === 0) {
          if (res.data.payStatus === 1) {
            // wx.showToast({title: '支付成功', icon: 'success', mark: true, duration: 2000})
            this.selectComponent("#toast").show({icon: "", txt: "支付成功", type: 'info', time: 1500});
            setTimeout(() => {this.setHiddentrue()}, 1500)
          } else {
            this.selectComponent("#toast").show({icon: "error", txt: res.data.reason, type: 'info', time: 2000});
            setTimeout(() => {this.setHiddentrue()}, 2000)
          }
        } else {
          this.selectComponent("#toast").show({icon: "error", txt: res.msg, type: 'info', time: 2000});
          setTimeout(() => {this.setHiddentrue()}, 2000)
        }
      }).catch(error => {
        this.selectComponent("#toast").show({icon: "error", txt: error, type: 'info', time: 2000});
        setTimeout(() => {this.setHiddentrue()}, 2000)
      })
    },
    clearInputValue() {
      this.inputValue = "";
      this.inputValueTrue = "";
    },
    focusTap() {
      this.isFocus = !this.isFocus
    },
    resetStatus() {
      this.inputValue = "";
      this.inputValueTrue = "";
      this.isFocus = false;
    },

    //关闭密码提示框
    closePassword() {
      this.setHiddenPassword()


    },
    noClosePassword() {
    },
    handleProxy() {
    }


  }
})
